package com.example.programming.sys.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.programming.sys.entity.subject.OneSubject;
import com.example.programming.sys.entity.subject.TwoSubject;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

import java.util.ArrayList;
import java.util.List;

import com.example.programming.common.Result;
import com.example.programming.sys.mapper.EduSubjectMapper;
import com.example.programming.sys.entity.EduSubject;

import com.example.programming.common.tools.BaseController;

/**
 * <p>
 * 课程科目 前端控制器 chrimer
 * </p>
 *
 * @author chrimer
 * @since 2020-09-04
 */
@RestController
@RequestMapping("/sys/edusubject")
@CrossOrigin
public class EduSubjectController extends BaseController {
    @Autowired
    private EduSubjectMapper eduSubjectMapper;

    @GetMapping("/getAllSubject")
    public Result getAllSubject() {
        Result result = new Result();
        QueryWrapper<EduSubject> wrapperOne = new QueryWrapper<>();
        wrapperOne.eq("parent_id", "0");
        List<EduSubject> subjectOne = eduSubjectMapper.selectList(wrapperOne);

        QueryWrapper<EduSubject> wrapperTwo = new QueryWrapper<>();
        wrapperTwo.ne("parent_id", "0");
        List<EduSubject> subjectTwo = eduSubjectMapper.selectList(wrapperTwo);

        List<OneSubject> finalList = new ArrayList<>();


        for (int i = 0; i < subjectOne.size(); i++) {
            EduSubject subject = subjectOne.get(i);
            OneSubject oneSubject = new OneSubject();
            BeanUtils.copyProperties(subject, oneSubject);


            List<TwoSubject> twoList = new ArrayList<>();
            for (int j = 0; j < subjectTwo.size(); j++) {
                EduSubject subject1 = subjectTwo.get(j);
                if (subject1.getParentId().equals(subject.getId())) {
                    TwoSubject twoSubject = new TwoSubject();
                    BeanUtils.copyProperties(subject1, twoSubject);
                    twoList.add(twoSubject);
                }
            }
            oneSubject.setChildren(twoList);
            finalList.add(oneSubject);
        }
        result.setData(finalList);
        return result;
    }


    @ApiImplicitParams({
    @ApiImplicitParam(name = "pageNum",paramType = "query",value = "当前页码",required = true),
    @ApiImplicitParam(name = "pageSize",paramType = "query",value = "每页显示记录数",required = true)
    })
    @ApiOperation(value = "分页获取EduSubject信息")
    @RequestMapping(method= RequestMethod.POST,value="/page")
    public Result page(
        Integer pageNum,
        Integer pageSize) {
        Result result = new Result();
        Page<EduSubject> page = new Page<EduSubject>(pageNum, pageSize);
        EduSubject parms = new EduSubject();
        QueryWrapper<EduSubject> warpper = new QueryWrapper<EduSubject>(parms);

        result.setData(eduSubjectMapper.selectPage(page, warpper));
        return result;
    }
    @ApiOperation(value = "保存修改EduSubject信息")
    @RequestMapping(method= RequestMethod.POST,value="/save")
    public Result save(@RequestBody EduSubject entity) {
        Result result = new Result();
        if (entity.getId() == null) {
            eduSubjectMapper.insert(entity);
        } else {
            eduSubjectMapper.updateById(entity);
        }
        return result;
    }
    @ApiOperation(value = "按id删除EduSubject，可以传入多个id用，隔开")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "ids",paramType = "query",value = "传入的id串，用，隔开",required = true)
    })
    @RequestMapping(method= RequestMethod.DELETE,value="/delete")
    public Result delete( String ids) {
        Result result = new Result();
        List<String> deleteIds = new ArrayList<String>();
        for (String id : ids.split(",")) {
            deleteIds.add(id);
        }
        eduSubjectMapper.deleteBatchIds(deleteIds);
        return result;
    }

}
